//
//  WQCHistooryViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/23.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCHistooryViewController.h"
#import "configuration.h"
#import "WQCHistoryTableViewCell.h"
#import "myModel.h"
#import "WQCPlanDetailedViewController.h"
@interface WQCHistooryViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView  *historyTableView;
@property(nonatomic,strong)NSMutableArray  *historyArr;

@end

@implementation WQCHistooryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _historyArr = [[NSMutableArray alloc]init];
    [self loadHistory];
    self.view.backgroundColor =WYCGlobalBg;
    [self setupNav];
    self.historyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, kScreenW, kScreenH) style:UITableViewStyleGrouped];
    self.historyTableView.dataSource = self;
    self.historyTableView.delegate = self;
    self.historyTableView.sectionFooterHeight = 10;
    self.historyTableView.sectionHeaderHeight = 10;
    self.historyTableView.backgroundColor = [UIColor clearColor];
    self.historyTableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.historyTableView];
    
}
- (void)setupNav
{
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    NSString  *tit = [NSString  stringWithFormat:@"%@(%@)-往期计划",_bankName,_lastNumber];
    self.navigationItem.title = tit;

}
-(void)loadHistory
{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = _cardid;
    
    [HttpTool POST:getCardAllPlan params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"往期计划返回%@",responseObject);
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultArray = responseObject[@"result"];
           for (NSDictionary * dict in resultArray)
           {
                myModel * selectionModel = [myModel mj_objectWithKeyValues:dict];
              [self.historyArr addObject:selectionModel];
            }
        }
        [self.historyTableView reloadData];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    

}

#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.historyArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString * strID = @"cell";
    
    WQCHistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil)
    {
        
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WQCHistoryTableViewCell" owner:self options:nil]lastObject];
        
    }
    cell.bigBg.frame =CGRectMake(10, 10, ScreenWidth-20, 140);
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    myModel * selectionModel = self.historyArr[indexPath.row];
   cell.amontLb.text =[NSString stringWithFormat:@"%.1f",[selectionModel.amount doubleValue] ];
    cell.timeLb.text = selectionModel.createTime;
    cell.balaceLb.text = [NSString stringWithFormat:@"%.1f",[selectionModel.balance doubleValue]];
    cell.pporgeLb.text =[NSString stringWithFormat:@"%.2f",[selectionModel.totalCharge doubleValue]];
   
    cell.planLb.text = [NSString stringWithFormat:@"%@天%@期",selectionModel.days, selectionModel.totalRepaymentNum];
    NSString  *state = selectionModel.state;

    if ([state isEqualToString:@"1"]) {
        cell.stateLb.text =@"待执行";
        cell.stateLb.textColor = wycMain;

    }
    else if ([state isEqualToString:@"2"])
    {
        
        cell.stateLb.text = @"执行中";
        cell.stateLb.textColor = wycMain;

        
    }
    else if ([state isEqualToString:@"3"])
    {
        cell.stateLb.text = @"执行成功";
        cell.stateLb.textColor = wycMain;

        
        
    }else if ([state isEqualToString:@"4"])
    {
        cell.stateLb.text = @"执行失败";
        cell.stateLb.textColor = [UIColor redColor];
    }else if ([state isEqualToString:@"5"])
    {
        cell.stateLb.text = @"还款终止";
        cell.stateLb.textColor = [UIColor redColor];

    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    myModel * selectionModel = self.historyArr[indexPath.row];

    WQCPlanDetailedViewController  *detailView= [[WQCPlanDetailedViewController alloc]init];
    detailView.cardid =selectionModel.cardId;
    detailView.planid = selectionModel.planId;
   [self.navigationController pushViewController:detailView animated:YES];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
